//
//  ViewController.swift
//  DingDingLaunch
//
//  Created by liguandong on 2024/3/12.
//

import UIKit
import SnapKit
class ViewController: UIViewController {
    let editw = 80
    let editH = 40
    var wakeUpDateLable : String{
        get {
            let date = viewModel.nextWakeUpDate // 假设为当前日期和时间
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MM月dd日 HH:mm"
            return dateFormatter.string(from: date)
        }
    }
    private lazy var dateTip: UILabel = {
        let v = UILabel()
        v.font = .systemFont(ofSize: 25)
        v.textColor = UIColor.white
        v.text = "下次触发时间:"
        return v
    }()
    
    private lazy var dateWakeUp: UILabel = {
        let v = UILabel()
        v.font = .systemFont(ofSize: 35)
        v.textColor = UIColor.red
        v.text = wakeUpDateLable
        return v
    }()
    
    lazy var keyCloseBtn: UIButton = {
        let v = UIButton()
        v.layer.cornerRadius = 10
        v.setBackgroundColor(color: .gray, forState: .normal)
        v.setBackgroundColor(color: .lightGray, forState: .highlighted)
//        v.backgroundColor = UIColor.red.withAlphaComponent(0.2)
        v.setTitleColor(.blue, for: .normal)
        v.setTitle("收起键盘", for: .normal)
        v.titleLabel?.font = .systemFont(ofSize: 30)
        v.addTarget(self, action: #selector(closeKeyBorder), for: .touchUpInside)
        return v
    }()
    
    lazy var openDingdingBtn: UIButton = {
        let v = UIButton()
        v.layer.cornerRadius = 10
//        v.backgroundColor = UIColor.red.withAlphaComponent(0.2)
        v.setBackgroundColor(color: .gray, forState: .normal)
        v.setBackgroundColor(color: .lightGray, forState: .highlighted)
        v.setTitleColor(.blue, for: .normal)
        v.setTitle("跳转dingding，务必点击一次", for: .normal)
        v.addTarget(self, action: #selector(openDingDing), for: .touchUpInside)
        return v
    }()
    
    lazy var skinTip: UILabel = {
         let v = UILabel()
        v.textColor = UIColor.white
        v.text = "跳过周六日"
        return v
    }()
    
    lazy var skinSwitch: UISwitch = {
        let v = UISwitch()
        v.isOn = viewModel.isSkipWeekdays
        v.addTarget(self, action: #selector(switchValueChanged), for: .valueChanged)
        return v
    }()
    
    // ShangBan设置
    lazy var atWorkTip: UILabel = {
         let v = UILabel()
        v.textColor = UIColor.white
        v.text = "ShangBan时间:"
        return v
    }()
    
    lazy var atWorkHourEdit: MyUITextField = {
       let v = MyUITextField()
        
        v.text = "\(viewModel.atWorkHour)"
        return v
    }()
    
    lazy var atWorkHour: UILabel = {
         let v = UILabel()
        v.textColor = UIColor.white
        v.text = "h"
        return v
    }()
    
    lazy var atWorkMinEdit: MyUITextField = {
       let v = MyUITextField()
        v.text = "\(viewModel.atWorkMin)"
        return v
    }()
    
    lazy var atWorkMin: UILabel = {
         let v = UILabel()
        v.textColor = UIColor.white
        v.text = "m"
        return v
    }()
    
    // Xiaban
    lazy var offWorkTip: UILabel = {
         let v = UILabel()
        v.textColor = UIColor.white
        v.text = "Xiaban时间"
        return v
    }()
    
    lazy var offWorkHourEdit: MyUITextField = {
       let v = MyUITextField()
        v.textColor = UIColor.white
        v.text = "\(viewModel.offWorkHour)"
        return v
    }()
    
    lazy var offWorkHour: UILabel = {
         let v = UILabel()
        v.textColor = UIColor.white
        v.text = "h"
        return v
    }()
    
    lazy var offWorkMinEdit: MyUITextField = {
       let v = MyUITextField()
        v.textColor = UIColor.white
        v.text = "\(viewModel.offWorkMin)"
        return v
    }()
    
    lazy var offWorkMin: UILabel = {
         let v = UILabel()
        v.textColor = UIColor.white
        v.text = "m"
        return v
    }()
    
    
    // ShangBan随机
    lazy var atWorkRandomTip: UILabel = {
         let v = UILabel()
        v.textColor = UIColor.white
        v.text = "ShangBan随机时间:"
        return v
    }()
    
    lazy var atWorkRandomMinEdit: MyUITextField = {
       let v = MyUITextField()
        v.textColor = UIColor.white
        v.text = "\(viewModel.offWorkRandomMin)"
        return v
    }()
    
    lazy var atWorkRandomMin: UILabel = {
         let v = UILabel()
        v.textColor = UIColor.white
        v.text = "m"
        return v
    }()
    
    // 下半随机
    lazy var offWorkRandomTip: UILabel = {
         let v = UILabel()
        v.textColor = UIColor.white
        v.text = "Xiaban随机时间:"
        return v
    }()
    
    lazy var offWorkRandomMinEdit: MyUITextField = {
       let v = MyUITextField()
        v.textColor = UIColor.white
        v.text = "\(viewModel.offWorkRandomMin)"
        return v
    }()
    
    lazy var offWorkRandomMin: UILabel = {
         let v = UILabel()
        v.textColor = UIColor.white
        v.text = "m"
        return v
    }()
    
    
    lazy var saveBtn: UIButton = {
        let v = UIButton()
        v.layer.cornerRadius = 10
//        v.backgroundColor = UIColor.red.withAlphaComponent(0.2)
        v.setBackgroundColor(color: .gray, forState: .normal)
        v.setBackgroundColor(color: .lightGray, forState: .highlighted)
        v.setTitleColor(.blue, for: .normal)
        v.setTitle("保存更新", for: .normal)
        v.titleLabel?.font = .systemFont(ofSize: 30)
        v.addTarget(self, action: #selector(save), for: .touchUpInside)
        return v
    }()
    
    private var viewModel = MainViewModel()
    
    
    
    
    @objc func closeKeyBorder(){
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
    
    @objc func openDingDing(){
        _ = openDingTalkUrl()
    }
    
    @objc func switchValueChanged(){
        viewModel.isSkipWeekdays =  skinSwitch.isOn
    }
    
    @objc func save(){
        
        viewModel.atWorkHour = Int(atWorkHourEdit.text!)!
        viewModel.atWorkMin = Int(atWorkMinEdit.text!)!
        viewModel.offWorkHour = Int(offWorkHourEdit.text!)!
        viewModel.offWorkMin = Int(offWorkMinEdit.text!)!
        viewModel.atWorkRandomMin = Int(atWorkRandomMinEdit.text!)!
        viewModel.offWorkRandomMin = Int(offWorkRandomMinEdit.text!)!
        
        viewModel.saveData()
        
        
        dateWakeUp.text = wakeUpDateLable
        
        viewModel.updateDate = {[weak self ] in
            if let self = self {
                debugPrint("更新时间")
                self.dateWakeUp.text = self.wakeUpDateLable
            }
        }
    }
    

    
    override func viewDidLoad() {
        super.viewDidLoad()
//        view.overrideUserInterfaceStyle = .dark
        // Do any additional setup after loading the view.
    
        view.backgroundColor = .black
    
        let bgView = UIView()
        bgView.backgroundColor = .gray
        view.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.width.equalToSuperview()
            make.height.equalTo(editH)
        }
        
        view.addSubview(dateTip)
        view.addSubview(dateWakeUp)
        
        dateTip.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(80)
            make.leading.equalToSuperview().offset(10)
        }
        dateWakeUp.snp.makeConstraints { make in
            make.left.equalTo(dateTip.snp.right).offset(5)
            make.centerY.equalTo(dateTip.snp.centerY)
        }
        view.addSubview(keyCloseBtn)
        keyCloseBtn.snp.makeConstraints { make in
            make.top.equalTo(dateTip.snp.bottom).offset(20)
            make.left.equalTo(dateTip.snp.left)
        }
        
        view.addSubview(openDingdingBtn)
        openDingdingBtn.snp.makeConstraints { make in
            make.height.equalTo(50)
            make.top.equalTo(keyCloseBtn.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
//            make.top.equalToSuperview().offset(1editH)
        }
        
        let parent = UIStackView()
        parent.axis = .vertical
        parent.spacing = 20
        view.addSubview(parent)
        parent.snp.makeConstraints { make in
            make.top.equalTo(openDingdingBtn.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
        }
        
        
        var statck = UIStackView()
        statck.spacing = 5
        statck.axis = .horizontal
        parent.addArrangedSubview(statck)
        statck.addArrangedSubview(skinTip)
        statck.addArrangedSubview(skinSwitch)
        
        statck = UIStackView()
        statck.spacing = 5
        statck.axis = .horizontal
        parent.addArrangedSubview(statck)
        statck.addArrangedSubview(atWorkTip)
        statck.addArrangedSubview(atWorkHourEdit)
        atWorkHourEdit.snp.makeConstraints { make in
            make.width.equalTo(editw)
            make.height.equalTo(editH)
        }
        statck.addArrangedSubview(atWorkHour)
        statck.addArrangedSubview(atWorkMinEdit)
        atWorkMinEdit.snp.makeConstraints { make in
            make.width.equalTo(editw)
            make.height.equalTo(editH)
        }
        statck.addArrangedSubview(atWorkMin)
        
        statck = UIStackView()
        statck.spacing = 5
        statck.axis = .horizontal
        parent.addArrangedSubview(statck)
        statck.addArrangedSubview(offWorkTip)
        statck.addArrangedSubview(offWorkHourEdit)
        offWorkHourEdit.snp.makeConstraints { make in
            make.width.equalTo(editw)
            make.height.equalTo(editH)
        }
        statck.addArrangedSubview(offWorkHour)
        statck.addArrangedSubview(offWorkMinEdit)
        offWorkMinEdit.snp.makeConstraints { make in
            make.width.equalTo(editw)
            make.height.equalTo(editH)
        }
        statck.addArrangedSubview(offWorkMin)
        
        statck = UIStackView()
        statck.spacing = 5
        statck.axis = .horizontal
        parent.addArrangedSubview(statck)
        statck.addArrangedSubview(atWorkRandomTip)
        statck.addArrangedSubview(atWorkRandomMinEdit)
        atWorkRandomMinEdit.snp.makeConstraints { make in
            make.width.equalTo(editw)
            make.height.equalTo(editH)
        }
        statck.addArrangedSubview(atWorkRandomMin)
        
        statck = UIStackView()
        statck.spacing = 5
        statck.axis = .horizontal
        parent.addArrangedSubview(statck)
        statck.addArrangedSubview(offWorkRandomTip)
        statck.addArrangedSubview(offWorkRandomMinEdit)
        offWorkRandomMinEdit.snp.makeConstraints { make in
            make.width.equalTo(editw)
            make.height.equalTo(editH)
        }
        statck.addArrangedSubview(offWorkRandomMin)
    
        
        view.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.height.equalTo(50)
            make.top.equalTo(parent.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
            make.width.equalTo(200)
        }
        
        
           // 添加点击手势识别器
           let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
           view.addGestureRecognizer(tapGesture)
           
           // 允许用户交互
           view.isUserInteractionEnabled = true
        
//        // 显示系统状态栏
//       setNeedsStatusBarAppearanceUpdate()
    }
    
    @objc func handleTap(_ sender: UITapGestureRecognizer) {
           // 处理点击事件
           print("View tapped")
   }
    
//    override var preferredStatusBarStyle: UIStatusBarStyle {
//           return .default
//       }
//
//   override var childForStatusBarStyle: UIViewController? {
//       return nil
//   }


    func onDataChange(){
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UIApplication.shared.isIdleTimerDisabled = true
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        UIApplication.shared.isIdleTimerDisabled = false
    }
}

class MyUITextField : UITextField{
    override init(frame: CGRect) {
        super.init(frame: frame)
        let v = self
        v.textAlignment = .center
        v.textColor = UIColor.white
        v.layer.borderWidth = 1
        v.layer.cornerRadius = 5
        v.layer.borderColor = UIColor.white.cgColor
        v.placeholder = "100"
        v.isEnabled = true
        v.keyboardType = .numberPad
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension UIButton {
    func setBackgroundColor(color: UIColor, forState state: UIControl.State) {
        UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
        UIGraphicsGetCurrentContext()?.setFillColor(color.cgColor)
        UIGraphicsGetCurrentContext()?.fill(CGRect(x: 0, y: 0, width: 1, height: 1))
        let backgroundImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        setBackgroundImage(backgroundImage, for: state)
    }
}
